package de.fsujena.chat_pi.server.userInterface;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.util.Date;

import de.fsujena.chat_pi.message.TextMessage;
import de.fsujena.chat_pi.server.Server;
import de.fsujena.chat_pi.server.user.User.ROLE;

public class ConsoleUI implements UserInterface {
	DateFormat df;
	BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
	Server serverObject;

	public ConsoleUI(Server serverObject) {
		df = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT );
		this.serverObject = serverObject;
	}

	@Override
	public void postTextMessage(TextMessage msg) {
		System.out.println("["+msg.getSenderUsername().toUpperCase()+" ("+df.format(msg.getReceived())+")]" + ">> " + msg.getMessageText());
	}

	@Override
	public void postInfo(String str) {
		System.out.println("[INFO ("+df.format(new Date())+")]" + ">> " + str);
	}

	@Override
	public void postWarning(String str) {
		System.out.println("[WARNING ("+df.format(new Date())+")]" + ">> " + str);
	}

	@Override
	public void postError(String str) {
		System.err.println("[ERROR ("+df.format(new Date())+")]" + ">> " + str);
	}

	@Override
	public void run() {		
		while (true) {
			String cmd = null;
			try {
				cmd = input.readLine();		//read from console

			} catch (Exception e) {
				e.printStackTrace();

			}

			if (cmd.startsWith("$")) {		//checks if input is a command
				commander(cmd);				//if yes, call commander
			} else if(serverObject.isStarted) {
				serverObject.networkListener.sendServerTextMessage(cmd);
			}
		}
	}
	
	/**
	 * process the user input
	 * 
	 * @param command contains the user input
	 */
	public void commander(String command) {
		
		String[] arg = command.split(" "); 					//splits input on every " " (arg[0] should contain"$")

		if (arg[1].equals("start") && arg.length >= 3 && arg[2].equals("server")) {				//starts the server
			if(arg.length==4){
				serverObject.startServer(arg[3]);
			}else{
				postError("invalid number of arguments");
			}
		} else if (arg[1].equals("make") && arg.length >= 4 && arg[3].equals("admin")) {			//set userrights to admin
			serverObject.userManager.setUserRole(arg[2], ROLE.ADMIN);
		} else if (arg[1].equals("make") && arg.length >= 4 && arg[3].equals("moderator")) {		//set userrights to moderator
			serverObject.userManager.setUserRole(arg[2], ROLE.MODERATOR);
		} else if (arg[1].equals("make") && arg.length >= 4 && arg[3].equals("normal")) {			//set userrights to normal
			serverObject.userManager.setUserRole(arg[2], ROLE.STANDARD);
		} else if (arg[1].equals("kick")&&arg[2].equals("user")) {									//kicks an user
			if(arg.length==4){
				serverObject.userManager.kickUser(arg[3]);
			} else {
				postError("invalid number of arguments");
			}
		} else if (arg[1].equals("bann")&&arg[2].equals("user")) {									//banns an user from the server
			if (arg.length==4) {
				serverObject.userManager.bannUser(arg[3]);
			} else {
				postError("invalid number of arguments");
			}
		} else if (arg[1].equals("unbann")&&arg[2].equals("user")) {								//unbann an user
			if (arg.length==4) {
				serverObject.userManager.unbannUser(arg[3]);
			} else {
				postError("invalid number of arguments");
			}
		} else if (arg[1].equals("remove")&&arg[2].equals("user")) {									//removes an user permanatly
			if (arg.length==4) {
				serverObject.userManager.removeUser(arg[3]);
			} else {
				postError("invalid number of arguments");
			}
		} else if (arg[1].equals("stop") && arg.length >= 3 && arg[2].equals("server")) {				//stops the server
			serverObject.stopServer();
		} else if (arg[1].equals("exit")) {										//exit the programm
			serverObject.exit();
		} else {
			System.out.println("command not known");
		}
	}

}
